package com.syezon.wifikey.pwd;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.syezon.android.base.utils.ToastUtils;
import com.syezon.wifikey.R;
import com.syezon.wifikey.app.WiFiKeyApp;
import com.syezon.wifikey.base.BaseActivity;
import com.syezon.wifikey.pwd.adapter.AddPwdAdapter;
import com.syezon.wifikey.pwd.dialog.AddWifiPwdDialog;
import com.syezon.wifikey.pwd.info.SaveWifiPwdInfo;
import com.syezon.wifikey.pwd.viewmodel.AddPwdViewModel;
import com.syezon.wifikey.pwd.viewmodel.MyPwdViewModel;
import com.syezon.wifikey.utils.StatusBarUtil;
import com.syezon.wifikey.wifi.info.WifiInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AddPwdActivity extends BaseActivity implements AddPwdAdapter.AddPwdListener {

    @BindView(R.id.rl_top)
    RelativeLayout mRlTop;
    @BindView(R.id.rv_wifi_list)
    RecyclerView mRvWifiList;
    AddPwdAdapter mAdapter;
    private AddWifiPwdDialog mAddWifiPwdDialog;

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_add_pwd;
    }

    @Override
    protected void initViewsAndEvents() {
        StatusBarUtil.setPaddingSmart(this, mRlTop);
        mRvWifiList.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
        mRvWifiList.addItemDecoration(dividerItemDecoration);
        mAdapter = new AddPwdAdapter(this);
        mAdapter.setListener(this);
        mRvWifiList.setAdapter(mAdapter);
        AddPwdViewModel addPwdViewModel = ViewModelProviders.of(this).get(AddPwdViewModel.class);
        addPwdViewModel.getWifiListLiveData().observe(this, new Observer<List<WifiInfo>>() {
            @Override
            public void onChanged(@Nullable List<WifiInfo> wifiInfos) {
                mAdapter.setData(wifiInfos);
            }
        });
        addPwdViewModel.getList();
    }

    @OnClick({R.id.ll_back, R.id.tv_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_add:
                showAddWifiPwdDialog();
                break;
        }
    }

    private void showAddWifiPwdDialog() {
        if (mAddWifiPwdDialog == null) {
            mAddWifiPwdDialog = new AddWifiPwdDialog(this, R.style.Dialog);
        }
        mAddWifiPwdDialog.show();
    }

    @Override
    public void addPwd(WifiInfo wifiInfo, String pwd) {
        if (pwd.length() < 8) {
            ToastUtils.showShort(R.string.pwd_short_tip);
            return;
        }
        SaveWifiPwdInfo saveWifiPwdInfo = new SaveWifiPwdInfo();
        saveWifiPwdInfo.setSsid(wifiInfo.getSsid());
        saveWifiPwdInfo.setPwd(pwd);
        saveWifiPwdInfo.setComment("");
        saveWifiPwdInfo.setConnectTime(0);
        saveWifiPwdInfo.setTime(System.currentTimeMillis());
        WiFiKeyApp.getDatabase().getWifiPwdDao().insertAll(saveWifiPwdInfo);
        List<WifiInfo> data = mAdapter.getData();
        int index = data.indexOf(wifiInfo);
        if (index >= 0) {
            data.remove(wifiInfo);
            mAdapter.notifyItemRemoved(index);
            MyPwdViewModel.getInstance().getSaveWifiList();
        }
    }
}
